﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar.SuperGrid;
using SCBX;

namespace SCBX
{
    public partial class GridDB : SuperGridControl
    {
        public GridDB()
        {
            InitializeComponent();
            //nhiều lựa chọn: TẮT
            this.PrimaryGrid.MultiSelect = false;
            //chế độ 1 click cho edit
            this.PrimaryGrid.MouseEditMode = MouseEditMode.SingleClick;            
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }

        private void GridDB_KeyUp(object sender, KeyEventArgs e)
        {
            //if (e.KeyCode == Keys.Enter)
            //{                
            //    if (this.PrimaryGrid.GetSelectedCells().Count < 1) { return; }
            //    GridCell gc = (GridCell)this.PrimaryGrid.GetSelectedCells()[0];
            //    if (gc.GridColumn.EditControl.GetType() == typeof(SCBX.GridMultiComboBox))
            //    {                    
            //        GridMultiComboBox gmcb = (GridMultiComboBox)gc.GridColumn.EditControl;                    
            //        e.Handled = true;
            //        e.SuppressKeyPress = true;
            //        //gmcb.ChooseOne();
            //    }
            //}
            if (e.KeyCode == Keys.Escape)
            {
                if (this.PrimaryGrid.GetSelectedCells().Count < 1) { return; }
                GridCell gc = (GridCell)this.PrimaryGrid.GetSelectedCells()[0];
                if (gc.GridColumn.EditControl.GetType() == typeof(SCBX.GridMultiComboBox))
                {
                    ((SCBX.GridMultiComboBox)gc.GridColumn.EditControl).Clear();
                    ((SCBX.GridMultiComboBox)gc.GridColumn.EditControl).ClosePopup();
                    this.OnValueColumnChanged(gc.RowIndex, gc.ColumnIndex);
                }
            }

            if (e.KeyCode == Keys.D && e.Control && this.PrimaryGrid.ReadOnly == false && this.PrimaryGrid.ActiveRow != null)
            {
                //GridElement gr = this.PrimaryGrid.GetSelectedRows()[0];
                int Rindex = this.PrimaryGrid.ActiveRow.RowIndex;

                GridRow gr = this.PrimaryGrid.InsertRow(this.PrimaryGrid.Rows.Count);

                for (int i = 0; i < this.PrimaryGrid.Columns.Count; i++)
                {
                    gr.Cells[i].Value = this.GetCell(Rindex, i).Value;
                }

            }
            else if (e.KeyCode == Keys.Delete && e.Shift && this.PrimaryGrid.ReadOnly == false && this.PrimaryGrid.ActiveRow != null)
            {
                //GridElement gr = this.PrimaryGrid.GetSelectedRows()[0];
                int Rindex = this.PrimaryGrid.ActiveRow.RowIndex;
                if (Rindex != this.PrimaryGrid.Rows.Count - 1) { this.PrimaryGrid.Rows.RemoveAt(Rindex); }
            }

            // Alt + T: Hiện menustrip
            else if (e.Alt && e.KeyCode == Keys.T)
            {
                if (this.ContextMenuStrip != null) { this.ContextMenuStrip.Show(); }
            }

            // Ctrl + F: Hiện menustrip
            // Hoặc mũi tên lên ở dòng đầu tiên để chuyển sang dòng tìm kiếm
            else if (e.Control && e.KeyCode == Keys.F)
            {
                if (this.PrimaryGrid.Filter.Visible) { this.PrimaryGrid.Filter.ActivateFilterEdit(this.PrimaryGrid.FirstVisibleColumn); }
            }            
        }

        private void GridDB_BeginEdit(object sender, GridEditEventArgs e)
        {
            if (e.EditControl.GetType() == typeof(SCBX.GridMultiComboBox))
            {
                GridMultiComboBox gmcb = (SCBX.GridMultiComboBox)e.EditControl;
                gmcb.RowIndex = e.GridCell.RowIndex;
                gmcb.ColumnIndex = e.GridCell.ColumnIndex;
                gmcb.Focus();

                if (e.GridCell.Value != null)
                {
                    if (e.GridCell.Value.ToString().Trim() != "") { gmcb.SelectedValue = e.GridCell.Value.ToString(); }
                    else { gmcb.Clear(); }
                }
                else
                {
                    gmcb.Clear();
                }
            }
        }

        private void GridDB_EndEdit(object sender, GridEditEventArgs e)
        {
            if (e.EditControl.GetType() == typeof(SCBX.GridMultiComboBox))
            {
                GridMultiComboBox gmcb = (GridMultiComboBox)e.EditControl;
                gmcb.ClosePopup();
            }
        }

        public delegate void ValueColumnChangedEventHandler(int rowindex, int columnindex);
        // Declare an event
        [Category("Action")]
        [Description("Fires when the value is changed")]
        public event ValueColumnChangedEventHandler ValueColumnChanged;

        protected virtual void OnValueColumnChanged(int rowindex, int columnindex)
        {
            if (ValueColumnChanged != null)
                ValueColumnChanged(rowindex,columnindex);
        }

        public void ValueColumnChanging(int rowindex, int columnindex)
        {
            this.OnValueColumnChanged(rowindex,columnindex);
        }

        public string GetCellVal(int rowindex, int columnindex)
        {
            //if (this.GetCell(rowindex, columnindex) == null) { return null; }
            if (this.GetCell(rowindex, columnindex).Value != null)
                if (this.GetCell(rowindex, columnindex).Value.ToString() != "")
                    return this.GetCell(rowindex, columnindex).Value.ToString();
            return null;
        }

        private void GridDB_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up && this.PrimaryGrid.Filter.Visible)
            {
                if (this.PrimaryGrid.GetSelectedRows().Count() == 0) { return; }
                GridRow gr = (GridRow)this.PrimaryGrid.GetSelectedRows()[0].GridPanel.ActiveRow;
                
                if (gr.RowIndex == 0) { this.PrimaryGrid.Filter.ActivateFilterEdit(this.PrimaryGrid.FirstVisibleColumn); }
            }
        }

    }
}
